/*
 * @Author: xiaoliang
 * @Date: 2025-03-05 14:45:20
 * @LastEditors: xiaoliang
 * @LastEditTime: 2025-03-05 14:45:22
 * @FilePath: /codepiece/quicksort.js
 * @Description: 快速排序
 */

function quickSort(arr) {
  if (arr.length <= 1) {
    return arr;
  }
  const pivot = arr[0];
  const left = [];
  const right = [];
  for (let i = 1; i < arr.length; i++) {
    if (arr[i] < pivot) {
      left.push(arr[i]);
    } else {
      right.push(arr[i]);
    }
  }
  return quickSort(left).concat(pivot, quickSort(right));
}

const sortedArr = quickSort([6, 4, 5, 3, 1, 8]);
console.log(sortedArr); // 输出升序数组